package com.mec.web.y2012.ebookshop.tags;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class WeekdayTag extends TagSupport {
	private static final long serialVersionUID = -3388614003220750022L;

	public void setDate(String date){
		this.date = date;
	}
	
	@Override
	public int doEndTag() throws JspException {
		GregorianCalendar cal = new GregorianCalendar();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		fmt.setLenient(true);
		
		//
		if(null != date && date.length() > 0){
			Date d = new Date();
			try{
				d = fmt.parse(date);
			}catch(Exception e){
				throw new JspException("Date parsing failed: " +
						e.getMessage());
			}
			cal.setTime(d);
		}
		//
		try{
			pageContext.getOut().print(WD[cal.get(Calendar.DAY_OF_WEEK)]);
		}catch(Exception e){
			throw new JspException("Weekday writing failed: "
					+ e.getMessage());
		}
		return EVAL_PAGE;
	}




	private static final String[] WD = {"",
		"Sun",
		"Mon",
		"Tue",
		"Wed",
		"Thu",
		"Fri",
		"Sat"};
	private String date;
}
